var query_stock_name = '';//全局变量，被搜索的股票名称
var query_ts_code = '';//全局变量，被搜索的股票ts代码
var query_search_cmd = '';//被搜索的内容

//窗体加载后函数，简写方式
function when_onload() {
     $.ajax({
        url: '/stock/inner_query/',
        type: "GET",

        success: function (data) {

            $("#div_test").html(encodeURI(data));
        }
    });

}

function query_daily(searchtxt){
    $.ajax({
        url: "/stock/query",
        type: "GET",
        data: {cmd_class: "query", command: "daily", stockname: searchtxt},
        success: function (callback) {
            var data = eval(callback);//解析json数据，json需要用eval函数解析后才方便使用
            var daily_column = eval(data['columns']);
            var daily_data = eval(data['data']);
            // $("#panel_daily").empty();
            $("#div_context").empty();
            $("#div_context").append("<div style='color: #ffffff' align='center'>每日交易信息</div>");

            // $("#panel_daily").append("<table id='table_daily'></table> ");
            $("#div_context").append("<table id='table_daily'></table> ");
            $("#table_daily").append("<tbody id='tbody_daily'></tbody> ");
            $("#tbody_daily").append("<tr id='tr_daily_title'></tr> ");
            for (i = 0; i < daily_column.length; ++i) {
                $("#tr_daily_title").append("<th scope='col'>" + daily_column[i].fields['remark'] + "</th> ");
            }

            for (i = 0; i < daily_data.length; ++i) {
                $("#tbody_daily").append("<tr></tr> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['ts_code'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['trade_date'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['open'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['high'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['low'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['close'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['pre_close'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['change'] + "</td> ");
                // $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['pct_chg'].toFixed(2) + "</td> ");
                if (daily_data[i].fields['pct_chg'].toFixed(2) >= 0) {
                    $("#tbody_daily:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['pct_chg'].toFixed(2) + "</td> ");
                } else {
                    $("#tbody_daily:last-child").append("<td style='color:greenyellow;'>" + daily_data[i].fields['pct_chg'].toFixed(2) + "</td> ");
                }
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['vol'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['amount'] + "</td> ");
            }

        },
        error: function () {
            alert('每日交易查询失败');
        }
    })
}

function query_daily_basic(searchtxt) {

    $.ajax({
        url: "/stock/query/",
        type: "GET",
        data: {cmd_class: "query", command: "daily_basic", stockname: searchtxt},
        success: function (callback) {
            var data = eval(callback);
            var daily_column = eval(data['columns']);
            var daily_data = eval(data['data']);
            // alert(daily_column);
            // alert(daily_data);

            // $("#panel_daily_basic").empty();
            // $("#panel_daily_basic").append("<table id='table_daily_baisc'></table> ");
            $("#div_context").empty();
            $("#div_context").append("<div style='color: #ffffff' align='center'>每日交易指标</div>");
            $("#div_context").append("<table id='table_daily_baisc'></table> ");
            $("#table_daily_baisc").append("<tbody id='tbody_daily_baisc'></tbody> ");
            $("#tbody_daily_baisc").append("<tr id='tr_daily_baisc_title'></tr> ");
            for (i = 0; i < daily_column.length; ++i) {
                $("#tr_daily_baisc_title").append("<th scope='col'>" + daily_column[i].fields['remark'] + "</th> ");
            }

            for (i = 0; i < daily_data.length; ++i) {
                $("#tbody_daily_baisc").append("<tr></tr> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['ts_code'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['trade_date'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['close'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['turnover_rate'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['turnover_rate_f'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['volume_ratio'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['pe'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['pe_ttm'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['pb'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['ps'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['ps_ttm'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['total_share'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['float_share'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['free_share'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['total_mv'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['circ_mv'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['dv_ratio'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['dv_ttm'] + "</td> ");
            }
        },
        error: function () {
            alert('获取交易指标失败');
        }
    })
}

function query_block_trade(searchtxt) {

    $.ajax({
        url: "/stock/query",
        type: "GET",
        data: {cmd_class: "query", command: "block_trade", stockname: searchtxt},
        success: function (callback) {
            var data = eval(callback);
            var daily_column = eval(data['columns']);
            var daily_data = eval(data['data']);
            // alert(daily_column);
            // alert(daily_data);

            // $("#panel_block_trade").empty();
            // $("#panel_block_trade").append("<table id='table_block_trade'></table> ");

            $("#div_context").empty();
            $("#div_context").append("<div style='color: #ffffff' align='center'>大宗交易</div>");
            $("#div_context").append("<table id='table_block_trade'></table> ");
            $("#table_block_trade").append("<tbody id='tbody_block_trade'></tbody> ");
            $("#tbody_block_trade").append("<tr id='tr_block_trade_title'></tr> ");
            for (i = 0; i < daily_column.length; ++i) {
                $("#tr_block_trade_title").append("<th scope='col'>" + daily_column[i].fields['remark'] + "</th> ");
            }

            for (i = 0; i < daily_data.length; ++i) {
                $("#tbody_block_trade").append("<tr></tr> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['ts_code'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['trade_date'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['price'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['vol'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['amount'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['buyer'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['seller'] + "</td> ");

            }
        },

        error: function () {
            alert('大宗交易查询失败');
        }
    })
}

function query_moneyflow(searchtxt) {

    $.ajax({
        url: "/stock/query",
        type: "GET",
        data: {cmd_class: "query", command: "moneyflow", stockname: searchtxt},
        success: function (callback) {
            var data = eval(callback);
            var daily_column = eval(data['columns']);
            var daily_data = eval(data['data']);
            // $("#panel_moneyflow").empty();
            // $("#panel_moneyflow").append("<table id='table_moneyflow'></table> ");

            $("#div_context").empty();
            $("#div_context").append("<div style='color: #ffffff' align='center'>资金流向</div>");
            $("#div_context").append("<table id='table_moneyflow'></table> ");
            $("#table_moneyflow").append("<tbody id='tbody_moneyflow'></tbody> ");
            $("#tbody_moneyflow").append("<tr id='tr_moneyflow_title'></tr> ");
            for (i = 0; i < daily_column.length; ++i) {
                $("#tr_moneyflow_title").append("<th scope='col'>" + daily_column[i].fields['remark'] + "</th> ");
            }

            for (i = 0; i < daily_data.length; ++i) {
                $("#tbody_moneyflow").append("<tr></tr> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['ts_code'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['trade_date'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_sm_vol'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_sm_amount'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_sm_vol'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_sm_amount'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_md_vol'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_md_amount'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_md_vol'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_md_amount'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_lg_vol'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_lg_amount'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_lg_vol'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_lg_amount'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_elg_vol'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_elg_amount'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_elg_vol'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_elg_amount'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['net_mf_vol'] + "</td> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['net_mf_amount'] + "</td> ");
                if (daily_data[i].fields['net_mf_amount'] > 0) {
                    $("#tbody_moneyflow:last-child").append("<td style='background-color: #dc3545'>" + daily_data[i].fields['net_mf_amount'] + "</td> ");
                } else {
                    $("#tbody_moneyflow:last-child").append("<td style='background-color: greenyellow'>" + daily_data[i].fields['net_mf_amount'] + "</td> ");
                }

            }
        },
        error: function () {
            alert('资金流查询失败');
        }
    })
}


window.onload = when_onload();



